﻿<%@ Page Language="C#" AutoEventWireup="true" CodePage="65001" %>

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"] == null || string.IsNullOrEmpty(Session["UserID"].ToString()))
        {
            Response.Redirect("index.html");
            return;
        }

        HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

        String Message = String.Empty;
        string file = DateTime.Now.ToString("yyyyMMddHHmmss")
            + (new Random()).Next(10000).ToString("D5")
            + hfc[0].FileName.Substring(hfc[0].FileName.LastIndexOf("."));
        string path = HttpContext.Current.Server.MapPath("~/baoming/images/") + file;

        HttpFileCollection files = HttpContext.Current.Request.Files;
        String fileName = System.IO.Path.GetFileName(hfc[0].FileName);
        HttpPostedFile postedFile = files[0];

        if (!String.IsNullOrEmpty(fileName))
        {
            //获取文件后缀名
            String str = fileName.Substring(fileName.Length - 4, 4).ToLower();
            if (str == ".jpg" || str == "jpeg" || str == ".bmp" || str == ".gif" || str == ".png")
            {
                try
                {
                    //上传文件
                    postedFile.SaveAs(path);
                    Response.Write("{'err':'0','msg':'" + file + "'}");
                    return;
                    //消息处理
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            else
            {
                //请确认上传的是Excel文件
                //Message= "只能上传.xls文件";
                Message = "只能上传图片！";
            }
        }
        else
        {
            //请选择要上传的文件
            //Message= "请选择要上传的文件！";
            Message = "请选择要上传的图片！";
        }
        Response.Write("{'err':'1','msg':'" + Message + "'}");
    }
    string jsonString(string str)
    {
        str = str.Replace("\\", "\\\\");
        str = str.Replace("/", "\\/");
        str = str.Replace("'", "\\'");
        return str;
    }
    
</script>
